/**
 * @description 将接口数据转换成 新的树数据
 * @param {Array} data - 树
 * @param {String} pre_index - 拼序号的
 * @param {Array} checkedData - 默认选中的
 * @returns {Array} 新的树数据
 */
const restructTreeData = function(data, pre_index, checkedData) {
	for (let i = 0; i < data.length; i++) {
		let v = data[i];
		if (pre_index) {
			v.index = pre_index + '.' + (i + 1);
		} else {
			v.index = i + 1;
		}
		v.key = v.id;
		v.title = v.text;
		v.scopedSlots = {
			title: 'title',
		};

		if (v.layers && v.layers.length) {
			let _contact = [...v.layers, ...v.children];
			v.children = Array.from(new Set(_contact));
			//初始化选中的值
			for (let j = 0; j < v.layers.length; j++) {
				if (v.layers[j].state.checked) {
					checkedData.push(v.layers[j].id);
				}
				v.layers[j].text = v.layers[j].nameCn; //
				v.layers[j].checked = v.layers[j].state.checked;
			}
		}
		if (v.children) {
			if (pre_index) {
				restructTreeData(
					v.children,
					pre_index + '.' + (i + 1),
					checkedData
				);
			} else {
				restructTreeData(v.children, i + 1, checkedData);
			}
		}
	}
	return {
		data,
		checkedData,
	};
};

/**
 * @description 提供mini tree 转换数据用
 * @param {Array} data
 * @returns {Array} 新的树数据
 */
const setTreeDetail = function(data) {
	for (let i = 0; i < data.length; i++) {
		data[i].text = data[i].label;
		if (!data[i].children) {
			let ly = CIM.layerTree._layerTreeData[data[i].id];
			for (let j in ly) {
				data[i][j] = ly[j];
			}
		} else {
			setTreeDetail(data[i].children);
		}
	}
	return data;
};

export { restructTreeData };
